package com.jw.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.system.entity.Customer;
import com.jw.system.entity.User;
import com.jw.system.entity.vo.UserVO;
import com.jw.system.mapper.CustomerMapper;
import com.jw.system.mapper.UserMapper;
import com.jw.system.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jw.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 客户表 服务实现类
 * </p>
 *
 * @author jw
 * @since 2021-04-20
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserService userService;

    @Override
    public Page<Customer> getCusList(Integer current, Integer size, Customer customer) {
        Page<Customer> page = new Page<>(current, size);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper();
        if (customer.getEmail() != null && !"".equals(customer.getEmail())) {
            queryWrapper.like("email", customer.getEmail());
        }
        if (customer.getUsername() != null && !"".equals(customer.getUsername())) {
            queryWrapper.like("username", customer.getUsername());
        }
        if (customer.getAddress() != null && !"".equals(customer.getAddress())) {
            queryWrapper.like("address", customer.getAddress());
        }
        if (customer.getSex() != null) {
            queryWrapper.like("sex", customer.getSex());
        }
        if (customer.getPhone() != null && !"".equals(customer.getPhone())) {
            queryWrapper.like("phone", customer.getPhone());
        }
        Page<Customer> pageList = customerMapper.selectPage(page, queryWrapper);
        return pageList;

    }

    @Override
    public List<Customer> getAllCus() {
        UserVO userVO = userService.getUserInfo();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper();
        if(userVO.getRoleId()!=1){
            queryWrapper.eq("user_id",userVO.getId());
        }
        List<Customer> customerList=customerMapper.selectList(queryWrapper);
        return customerList;
    }
}
